package com.feicheng.iou.controller.weChatSendMessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.openMessage.util.WechatSendMessageConfig;
import com.feicheng.iou.util.HttpClientUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/iou")
@Slf4j
public class WechatSendMessageController {

    @RequestMapping("/weChatPushMessage")
    public void pushMessage(){
        String accessTokenUrl = WechatSendMessageConfig.GET_ACCESSTOKEN_GATEWAY
                +"?appid="+ WechatSendMessageConfig.APPID
                +"&secret="+WechatSendMessageConfig.APPSECRET
                +"&grant_type=client_credential";
        String result = null;
        try {
            result = new HttpClientUtils().get(accessTokenUrl);
            log.info("获取到的access_token为"+result);
            Gson gson = new Gson();
            Map<String,Object> resultMap = gson.fromJson(result, HashMap.class);
            String access_token = (String) resultMap.get("access_token");
            log.info(access_token);

            //发起发送模板消息请求
            String postUrl = WechatSendMessageConfig.SEND_MESSAGE_GATEWAY
                            +"?access_token="+access_token;

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("touser", "oP-t86YHfUP_de0tJpAV_VqypkgI"); //贾志伟在戍码助学的openid
            jsonObject.put("template_id", "-tgKn_kSRCb0Qf-THJER-WWGvfpyX-y1TlZyWOHCU18"); //模板Id  https://mp.weixin.qq.com/advanced/tmplmsg?action=edit&id=-tgKn_kSRCb0Qf-THJER-WWGvfpyX-y1TlZyWOHCU18&token=1784003957&lang=zh_CN
            jsonObject.put("url", "http://fq.kerchin.xyz");

            JSONObject data = new JSONObject();
            JSONObject first = new JSONObject();
            //first
            first.put("value", "您好xxx,还款账单已经生成,请前往公众号查看详情,点击菜单跳转到支付页面");
            first.put("color", "#173177");

            //keyword1 缴费金额
            JSONObject keyword1 = new JSONObject();
            keyword1.put("value", "500元");
            keyword1.put("color", "#173177");

            //keyword2 缴费时间
            JSONObject keyword2 = new JSONObject();
            keyword2.put("value", "2021年9月16日");
            keyword2.put("color", "#173177");

            //remark
            JSONObject remark = new JSONObject();
            remark.put("value", "请您即时缴费");
            remark.put("color", "#173177");

            //组装JSONObject
            data.put("first", first);
            data.put("keyword1", keyword1);
            data.put("keyword2", keyword2);
            data.put("remark", remark);
            jsonObject.put("data", data);


            //发起发送消息的请求,走你
            String string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toJSONString());
            log.info("String===>"+string);
            JSONObject response = JSON.parseObject(string);
            Integer errcode = response.getIntValue("errcode");

            if(errcode == 0){
                //发送成功
                log.info("发送成功");
            }else {
                //发送失败
                log.info("发送失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
